package com.lkd.controller;

import com.lkd.dto.SkuDTO;
import com.lkd.dto.SkuQueryDTO;
import com.lkd.entity.Sku;
import com.lkd.result.TableInfo;
import com.lkd.service.SkuService;
import com.lkd.utils.AliOssUtil;
import com.lkd.vo.SkuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/16 21:25
 * @Description:
 **/
@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
public class SkuController {

    private final SkuService skuService;
    private final AliOssUtil aliOssUtil;
    @Autowired
    public SkuController(SkuService skuService, AliOssUtil aliOssUtil) {
        this.skuService = skuService;
        this.aliOssUtil = aliOssUtil;
    }

    /**
     * 保存商品信息
     */
    @PostMapping
    public boolean save(@RequestBody SkuDTO skuDto){
        log.info("新增商品：{}",skuDto);
        skuService.save(skuDto);
        return true;
    }

    /**
     * 文件上传
     */
    @PostMapping( "/fileUpload")
    public String upload(MultipartFile fileName) throws IOException {
        log.info("上传文件：{}",fileName);
        return aliOssUtil.upload(fileName.getBytes(),fileName.getOriginalFilename());
    }
    /**
     * 获取商品列表搜索
     */
    @GetMapping ("/search")
    public TableInfo<Sku> list(SkuQueryDTO sku){
        log.info("商品搜索:{}",sku);
        return skuService.list(sku);
    }
    /**
    * @Description 更新商品
    * @Param [skuId]
    * @Date 2025/8/17 10:36
    **/
    @PutMapping ("/{skuId}")
    public boolean updateSku(@PathVariable String skuId,@RequestBody SkuDTO skuDto){
        log.info("获取商品:{},{}",skuId,skuDto);
        skuDto.setSkuId(skuId);
        skuService.updateSku(skuDto);
        return true;
    }
    /**
    * @Description 获取销量前十的商品列表
    * @Param [skuDto]
    * @Date 2025/8/17 10:36
    **/
    @GetMapping("/businessTop10/{businessId}")
    public List<SkuVO> top10(@PathVariable String businessId){
        log.info("获取业务{}的top10商品",businessId);
        return skuService.listTop10(businessId);
    }
}
